//
//  LCHomePageViewController.m
//  段子手
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCHomePageViewController.h"

@interface LCHomePageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIButton *button0;
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIButton *button3;
@property (nonatomic, retain) UIButton *button4;


@property (nonatomic, retain) UICollectionView *collectionView;




@end

@implementation LCHomePageViewController

#define TEM (SCREENW - 80)/ 4

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1 green:0.87 blue:0.68 alpha:1];
    
    
    [self createTopView];
    
    [self createCollectionView];
    
    
}

#pragma mark - 创建collectionView
- (void)createCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(SCREENW, SCREENH - 49 - 64 - 40);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, SCREENW, SCREENH - 49 - 64 - 40) collectionViewLayout:layout];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[LCHomeCollectionViewCell class] forCellWithReuseIdentifier:@"homeCollectionViewCell"];
    [_collectionView registerClass:[LCEssenceCollectionViewCell class] forCellWithReuseIdentifier:@"essenceCell"];
    
    [self.view addSubview:_collectionView];
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    
    if (indexPath.row == 0) {
        
        LCHomeCollectionViewCell * homeCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionViewCell" forIndexPath:indexPath];
        
        homeCollectionViewCell.backgroundColor = [UIColor clearColor];
        
        homeCollectionViewCell.StrForGet = @"http://lf.snssdk.com/neihan/stream/mix/v1/?mpic=1&webp=1&essence=1&content_type=-101&message_cursor=22993292&longitude=121.53942990089&latitude=38.882784070399&bd_longitude=121.544414&bd_latitude=38.883522&bd_city=大连市&am_longitude=121.544248&am_latitude=38.883352&am_city=大连市&am_loc_time=1458305265881&count=30&min_time=1458292078&screen_width=1080&iid=3599529901&device_id=5493634716&ac=wifi&channel=meizu&aid=7&app_name=joke_essay&version_code=500&version_name=5.0.0&device_platform=android&ssmix=a&device_type=M353&os_api=19&os_version=4.4.4&uuid=861138023246677&openudid=13d3ef14d7878c0f&manifest_version_code=500";
        
        
        [homeCollectionViewCell addHeader];
        [homeCollectionViewCell addFooter];
            return homeCollectionViewCell;
        
    }else if (indexPath.row == 1) {

        LCHomeCollectionViewCell * homeCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionViewCell" forIndexPath:indexPath];
        
        homeCollectionViewCell.backgroundColor = [UIColor clearColor];

        homeCollectionViewCell.StrForGet = @"http://lf.snssdk.com/neihan/stream/mix/v1/?mpic=1&webp=1&essence=1&content_type=-104&message_cursor=22993292&longitude=121.53942396028&latitude=38.882660904674&bd_longitude=121.544404&bd_latitude=38.883489&bd_city=大连市&am_longitude=121.54436&am_latitude=38.883445&am_city=大连市&am_loc_time=1458703109842&count=30&min_time=1458199081&screen_width=1080&iid=3599529901&device_id=5493634716&ac=wifi&channel=meizu&aid=7&app_name=joke_essay&version_code=500&version_name=5.0.0&device_platform=android&ssmix=a&device_type=M353&os_api=19&os_version=4.4.4&uuid=861138023246677&openudid=13d3ef14d7878c0f&manifest_version_code=500";
        
        [homeCollectionViewCell addHeader];
        [homeCollectionViewCell addFooter];
        
        return homeCollectionViewCell;

    }else if (indexPath.row == 2){
        
        LCHomeCollectionViewCell * homeCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionViewCell" forIndexPath:indexPath];
        
        homeCollectionViewCell.backgroundColor = [UIColor clearColor];
        homeCollectionViewCell.StrForGet = @"http://lf.snssdk.com/neihan/stream/mix/v1/?mpic=1&webp=1&essence=1&content_type=-103&message_cursor=22993292&longitude=121.53942396028&latitude=38.882660904674&bd_longitude=121.544407&bd_latitude=38.883458&bd_city=大连市&am_longitude=121.544373&am_latitude=38.883437&am_city=大连市&am_loc_time=1458716047371&count=30&min_time=1458716108&screen_width=1080&iid=3599529901&device_id=5493634716&ac=wifi&channel=meizu&aid=7&app_name=joke_essay&version_code=500&version_name=5.0.0&device_platform=android&ssmix=a&device_type=M353&os_api=19&os_version=4.4.4&uuid=861138023246677&openudid=13d3ef14d7878c0f&manifest_version_code=500";
        
        [homeCollectionViewCell addHeader];
        [homeCollectionViewCell addFooter];
        
        return homeCollectionViewCell;
    }else if (indexPath.row == 3){
        LCHomeCollectionViewCell * homeCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCollectionViewCell" forIndexPath:indexPath];
        
        homeCollectionViewCell.backgroundColor = [UIColor clearColor];
        homeCollectionViewCell.StrForGet = @"http://lf.snssdk.com/neihan/stream/mix/v1/?mpic=1&webp=1&essence=1&content_type=-102&message_cursor=22993292&longitude=121.53942396028&latitude=38.882660904674&bd_longitude=121.544407&bd_latitude=38.883458&bd_city=大连市&am_longitude=121.544373&am_latitude=38.883437&am_city=大连市&am_loc_time=1458716047371&count=30&min_time=1458132066&screen_width=1080&iid=3599529901&device_id=5493634716&ac=wifi&channel=meizu&aid=7&app_name=joke_essay&version_code=500&version_name=5.0.0&device_platform=android&ssmix=a&device_type=M353&os_api=19&os_version=4.4.4&uuid=861138023246677&openudid=13d3ef14d7878c0f&manifest_version_code=500";
        
        [homeCollectionViewCell addHeader];
        [homeCollectionViewCell addFooter];
                return homeCollectionViewCell;
        
    }else if (indexPath.row == 4){
        
        LCEssenceCollectionViewCell *essecceCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"essenceCell" forIndexPath:indexPath];
        
//        essecceCell.StrForGet = @"http://toutiao.com/ma/?media_id=5234320301&count=10&max_behot_time=0&callback=jsonp1&csrfmiddlewaretoken=undefined&_=1458717352851";
        
        return essecceCell;
        
    }else return nil;
    
    
}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 5;
}



#pragma mark - 创建放button的topView
- (void)createTopView{
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 40)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    self.button0 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button0.frame = CGRectMake(20, 5, 40, 30);
    _button0.backgroundColor = [UIColor clearColor];
    [_button0 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button0 setTitle:@"推推" forState:UIControlStateNormal];
    _button0.titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_button0];
    [_button0 addTarget:self action:@selector(b0Action:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button1.frame = CGRectMake(20 + TEM, 5, 40, 30);
    _button1.backgroundColor = [UIColor clearColor];
    [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button1 setTitle:@"视频" forState:UIControlStateNormal];
     _button1.titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_button1];
    [_button1 addTarget:self action:@selector(b1Action:) forControlEvents:UIControlEventTouchUpInside];

    
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button2.frame = CGRectMake(20 + TEM * 2, 5, 40, 30);
    _button2.backgroundColor = [UIColor clearColor];
    [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button2 setTitle:@"囧图" forState:UIControlStateNormal];
     _button2.titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_button2];
    [_button2 addTarget:self action:@selector(b2Action:) forControlEvents:UIControlEventTouchUpInside];

    
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button3.frame = CGRectMake(20 + TEM * 3, 5, 40, 30);
    _button3.backgroundColor = [UIColor clearColor];
    [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button3 setTitle:@"段子" forState:UIControlStateNormal];
     _button3.titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_button3];
    [_button3 addTarget:self action:@selector(b3Action:) forControlEvents:UIControlEventTouchUpInside];

    
    self.button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button4.frame = CGRectMake(20 + TEM * 4, 5, 40, 30);
    _button4.backgroundColor = [UIColor clearColor];
    [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button4 setTitle:@"你看" forState:UIControlStateNormal];
     _button4.titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:_button4];
    [_button4 addTarget:self action:@selector(b4Action:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
}


- (void)b0Action:(UIButton *)button{
    [_button0 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
//    _collectionView.contentOffset = CGPointMake(0, 0);
    
    
    if (_collectionView.contentOffset.x == SCREENW ) {
          [_collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [_collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
    }

    

    
}


- (void)b1Action:(UIButton *)button{
    [_button1 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
//    _collectionView.contentOffset = CGPointMake(SCREENW, 0);
    
    if (_collectionView.contentOffset.x == 0 || _collectionView.contentOffset.x == SCREENW * 2) {
               [_collectionView setContentOffset:CGPointMake(SCREENW, 0) animated:YES];
    }else{
          [_collectionView setContentOffset:CGPointMake(SCREENW, 0) animated:NO];
    }
    
    
    
}


- (void)b2Action:(UIButton *)button{
    [_button2 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    if (_collectionView.contentOffset.x == SCREENW || _collectionView.contentOffset.x == SCREENW * 3) {
    [_collectionView setContentOffset:CGPointMake(SCREENW * 2, 0) animated:YES];
    }else{
        [_collectionView setContentOffset:CGPointMake(SCREENW * 2, 0) animated:NO];
    }
    

    
}


- (void)b3Action:(UIButton *)button{
    [_button3 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    if (_collectionView.contentOffset.x == SCREENW * 2 || _collectionView.contentOffset.x == SCREENW * 4) {
          [_collectionView setContentOffset:CGPointMake(SCREENW * 3, 0) animated:YES];
    }else{
        [_collectionView setContentOffset:CGPointMake(SCREENW * 3, 0) animated:NO];
    }
    
    
    
}


- (void)b4Action:(UIButton *)button{
    [_button4 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    
    if (_collectionView.contentOffset.x == SCREENW * 3 ) {
        [_collectionView setContentOffset:CGPointMake(SCREENW * 4, 0) animated:YES];

    }else{
        [_collectionView setContentOffset:CGPointMake(SCREENW * 4, 0) animated:NO];
    }
    
    
}

//- (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset{
//
//    NSLog(@"@@@~~~");
//    
//    return CGPointMake(0, 0);
//}

 
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
      if (_collectionView.contentOffset.x == 0 ) {
          [_button0 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
          [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          
      }else if (_collectionView.contentOffset.x == SCREENW ){
          [_button1 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
          [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          
          
          
      }else if (_collectionView.contentOffset.x == SCREENW * 2){
          [_button2 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
          [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          
          
      }else if (_collectionView.contentOffset.x == SCREENW * 3){
          
          [_button3 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
          [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          
      }else if (_collectionView.contentOffset.x == SCREENW * 4){
          [_button4 setTitleColor:[UIColor colorWithRed:0.99 green:0.16 blue:0.4 alpha:1] forState:UIControlStateNormal];
          [_button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          [_button0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          
      }
    
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end